<!--
 * @Author: gyc
 * @Date: 2020-06-02 10:52:15
 * @LastEditTime: 2020-08-24 08:44:26
 * @Description: Nuxt测试版
--> 
<template>
  <div class="test-box">
    <div class="tucao-run">
      <h3>今日吐槽</h3>
      <div class="tucao">
        <img src="/img/logo.png" />f撒大苏打 阿萨大时代阿松大阿啊实打实大厦啊实打实大萨达是的松大阿萨爱上凡人歌给俄国人二二而且日期认同感额！
      </div>
      <div class="tucao"> <img src="/img/logo.png" />1.f撒大苏打 阿萨大时代阿松大阿松大阿萨爱上凡人歌给俄国人二二而且日期认同感额！</div>
      <div class="tucao"> <img src="/img/logo.png" />1.f撒大苏打 阿萨大时代阿松大阿松大阿萨爱上凡人歌给俄国人二二而且日期认同感额！</div>
    </div>
  </div>
</template>

<script>
import io from "socket.io-client";
export default {
  name: "testBox",
  layout: "dpage",
  data() {
    return {
      pageOk: false,
    };
  },
  mounted() {
    this.pageOk = true;
    this.WebSocketTest();
  },
  methods: {
    WebSocketTest() {
      if ("WebSocket" in window) {
        console.log("您的浏览器支持 WebSocket!");

        // 打开一个 web socket
        var socket = io("http://localhost:7001", {
          query: {
            userid: 16,
          },
          transports: ["websocket"],
        });
        socket.on("connect", function () {
          const id = socket.id;
          socket.emit("chat", "world");
          console.log("#connect,", id, socket);

          // 监听自身 id 以实现 p2p 通讯
          socket.on(id, (msg) => {
            console.log("#receive,", msg);
          });
        });
        socket.on("online", (msg) => {
          console.log("#online,", msg);
        });
        socket.on("res", (msg) => {
          console.log("#res,", msg);
        });
        socket.on("event", function (data) {
          console.log("#event", data);
        });
        socket.on("disconnect", function () {});
      } else {
        // 浏览器不支持 WebSocket
        console.log("您的浏览器不支持 WebSocket!");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.test-box {
  position: relative;
  height: 100vh;
  padding-top: 20px;
  .tucao-run {
    display: flex;
    flex-direction: column;
    position: relative;
    align-items: center;
    width: 100%;
    height: 100%;
    h3 {
      margin-bottom: 15px;
    }
    .tucao {
      display: flex;
      align-items: center;
      color: black;
      font-size: 16px;
      letter-spacing: 1px;
      margin-bottom: 20px;
      width: 700px;
      padding: 10px;
      img {
        height: 40px;
        width: 40px;
        margin-right: 10px;
      }
    }
  }
}
</style>